//
//  OEPakageHomeViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/4.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OERechargeDepositViewController.h"
#import "OETransactionDetailViewController.h"
@interface OERechargeDepositViewController ()
@property(nonatomic, strong) STLabel                     *moneyLable;/**<  */
@property(nonatomic, strong) NSArray                     *dataSouce;/**< <##> */
@end

@implementation OERechargeDepositViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];

    
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"PAYPAL") accessoryView:[self buttonWithSelcted:YES]],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"CREDIT CARD") accessoryView:[self buttonWithSelcted:NO]],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"BANK DEPOSIT") accessoryView:[self buttonWithSelcted:NO]]
                       ];
    
    [OEUserDataController sendGetPayMethod:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            NSArray * array = (id)resp;
            NSMutableArray * dataSouce = NSMutableArray.new;
            for (NSDictionary * dic in array) {
                NSString * pay_name = [dic[@"pay_name"] description];
                NSString * pay_id  = [dic[@"pay_id"] description];
                //                NSString * pay_code;
                BOOL selected = NO;
                if ([array indexOfObject:dic] == 0) {
                    selected = YES;
                }
                
                STTableEasyModel * model = [[STTableEasyModel alloc] initWithTextString:pay_name accessoryView:[self buttonWithSelcted:selected]];
                model.isSlected = selected;
                [dataSouce addObject:model];
                model.obj_id = pay_id;
            }
            self.dataSouce = dataSouce.copy;
            [self.tableView reloadData];
        }
    }];
    
    // Do any additional setup after loading the view.
}
- (STButton*)buttonWithSelcted:(BOOL)selected{
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"36×36未选中"]];
    buyButton.userInteractionEnabled = NO;
    [buyButton setImage:[UIImage imageNamed:@"34×34选中"] forState:UIControlStateSelected];
    buyButton.selected = selected;
    return buyButton;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [OEUserDataController sendFetcBeVipMoneyRequest:^(NSString * _Nonnull money) {
        self.moneyLable.text = [NSString stringWithFormat:@"$%@",money];
    }];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 110)];
    header.backgroundColor = UIColor.whiteColor;
    self.tableView.backgroundColor = UIColor.whiteColor;
    __weak typeof(self) weakSelf =  self;
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 30, UIScreenWidth - 20, 40)
                                                     text:OESTRING(@"RECHARGE DEPOSIT")
                                                textColor:UIColor.blackColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, titleLable.bottom + 20, UIScreenWidth - 40, 130)];
    imageView.image = [UIImage imageNamed:@"750×320背景"];
    [header addSubview:imageView];
    imageView.userInteractionEnabled = YES;
    imageView.layer.cornerRadius = 4;
    imageView.clipsToBounds = YES;
    [imageView st_showRoundLayer];
    
    STLabel * stitleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 15, UIScreenWidth - 20, 40)
                                                      text:OESTRING(@"DEPOSIT BALANCE")
                                                 textColor:OE_LIMITE_COLOR
                                                      font:18
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [imageView addSubview:stitleLable];
    self.moneyLable = stitleLable;
    
    STLabel * moneyLable = [[STLabel alloc] initWithFrame:CGRectMake(10, stitleLable.bottom + 20, UIScreenWidth - 20, 40)
                                                     text:OESTRING(@"Deposit")
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [imageView addSubview:moneyLable];

    
    header.height = imageView.bottom   + 20;
    self.tableView.tableHeaderView = header;
    
    STButton * payButton = [[STButton alloc] initWithFrame:CGRectMake(40, 0, UIScreenWidth - 80, 44)
                                                    title:OESTRING(@"PAYMENT")
                                               titleColor:UIColor.whiteColor
                                                titleFont:15
                                             cornerRadius:22
                                          backgroundColor:OE_LIMITE_COLOR
                                          backgroundImage:nil
                                                    image:nil];
    payButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    payButton.top = TMUtils.tabBarTop - 40;
    [payButton makeImageRight];
    [self.view addSubview:payButton];
    [payButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedPayButton];
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, TMUtils.navgationBarBootom, UIScreenWidth, payButton.top - TMUtils.navgationBarBootom);
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    for (STTableEasyModel * emodel in self.dataSouce) {
        UIButton * button = (id)emodel.accessoryView;
        button.selected = NO;
    }
    UIButton * button = (id)model.accessoryView;
    button.selected = YES;
    [self.tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
  
}
- (void)onSelctedPayButton{
    NSString * money = [self.moneyLable.text stringByReplacingOccurrencesOfString:@"$" withString:@""];
    if (!money.length) {
        [SVProgressHUD showInfoWithStatus:OESTRING(@"Please input or chose money")];
        return;
    }
    NSString * pay_id;
    for (STTableEasyModel * model in self.dataSouce) {
        if (model.isSlected) {
            pay_id = model.obj_id;
        }
    }
    
    [OEUserDataController sendPayRequestWithMoney:money pay_id:pay_id handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:OESTRING(@"Pay Success")];
            OEPaySuccessViewController * vc =  OEPaySuccessViewController.new;
            vc.recharge_id = [resp[@"recharge_id"] description];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}
@end
